using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MapMaker
{
    public partial class frmDirections : Form
    {
        private frmMain parentForm = null;
        private ArrayList route = null;
        private string distanceUnit = null;
        
        public frmDirections()
        {
            InitializeComponent();
        }

        public string TotalDistance
        {
            set
            {
                lblTotalDistance.Text = value;
            }
        }

        public string DistanceUnit
        {
            get
            {
                return distanceUnit;
            }
        }

        public string TotalTime
        {
            set
            {
                lblTotalTime.Text = value;
            }
        }

        public ArrayList Route
        {
            get
            {
                return route;
            }
            set
            {
                route = value;

                lvwDirections.Items.Clear();
                ListViewItem item = null;

				for (int i = 0; i < route.Count; i++)
				{
					string[] line = (string[])route[i];

                    item = new ListViewItem(line[3]);

					if (i == 0 || 
						i == route.Count - 1 ||
						string.IsNullOrEmpty(line[2]) ||
						line[2].ToLower().Equals("null") ||
						line[2].Equals("0"))
					{
						item.Font = new Font(this.Font, FontStyle.Bold);
                        lvwDirections.Items.Add(item);
					}
					else
                    {
                        item.SubItems.Add(line[2]);
                        lvwDirections.Items.Add(item);
					}
				}

				btnGetDirections.Enabled = true;
                btnGetDirections.Text = "Get Directions";
                txtFrom.Enabled = true;
                txtTo.Enabled = true;
                cboType.Enabled = true;
                cboUnit.Enabled = true;
                parentForm.ExportToPOIMenu.Enabled = true;
            }
        }

        private void btnGetDirections_Click(object sender, EventArgs e)
        {
            bool validationError = false;

            if (string.IsNullOrEmpty(txtFrom.Text.Trim()))
            {
                lblFrom.ForeColor = Color.Red;
                validationError = true;
            }

            if (string.IsNullOrEmpty(txtTo.Text.Trim()))
            {
                lblTo.ForeColor = Color.Red;
                validationError = true;
            }

            if (!validationError)
            {
                lblTo.ForeColor = Color.Black;
                lblFrom.ForeColor = Color.Black;
                btnGetDirections.Enabled = false;
                txtFrom.Enabled = false;
                txtTo.Enabled = false;
                cboType.Enabled = false;
                cboUnit.Enabled = false;
                btnGetDirections.Text = "Getting Directions...";

                distanceUnit = cboUnit.SelectedItem.ToString();

                parentForm.MapViewerForm.GetDirections(txtFrom.Text, txtTo.Text, cboUnit.SelectedItem.ToString().ToLower(), cboType.SelectedItem.ToString().ToLower());
            }
        }

        private void frmDirections_Load(object sender, EventArgs e)
        {
			parentForm = (frmMain)this.MdiParent;
			
            cboType.SelectedIndex = 0;
            cboUnit.SelectedIndex = 0;
        }
    }
}